﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Reflection;
using System.Windows.Forms;
using Automation.Model;
using System.Drawing;

namespace SerializableEditor
{
    class Common
    {
        public static Object data = new object();

        public static List<T> GetObjType<T>(object obj)
        {
            List<T> result = new List<T>();
            Type type = obj.GetType();
            string Name = obj.GetType().ToString();
            result = (List<T>)obj;
            return result;
        }

        public static List<PropertyInfo> GetObjProperties<T>(T obj)
        {
            List<PropertyInfo> result = new List<PropertyInfo>();
            PropertyInfo[] props = null;
            Type ObjType = obj.GetType();
            props = ObjType.GetProperties();
            foreach (var item in props)
            {
                result.Add(item);
            }
            return result;
        }

        public static void GetType(object obj, DataGridView table)
        {
            string type = "";
            var name = obj.GetType().Name;
            var tmp = obj.GetType().GetGenericArguments();
            if (tmp.Length > 0)
            {
                type = tmp[0].FullName;
            }
            else
            {
                type = obj.GetType().ToString();
            }

            #region Yss
            if (type == ("Automation.Model.Yss"))
            {
                List<Yss> yss = new List<Yss>();

                if (name != "List`1")
                {
                    yss.Add((Yss)obj);
                }
                else
                {
                    yss = (List<Yss>)obj;
                }
                AddDataToTable(yss, table);
            }
            #endregion
            #region Gc_Dezc
            else if (type == "Automation.Model.Gc_Dezc")
            {
                List<Gc_Dezc> yss = new List<Gc_Dezc>();
                if (name != "List`1")
                {
                    yss.Add((Gc_Dezc)obj);
                }
                else
                {
                    yss = (List<Gc_Dezc>)obj;
                }

                AddDataToTable(yss, table);
            }
            #endregion
            #region CsYss
            else if (type == "Automation.Model.CsYss")
            {
                List<CsYss> yss = new List<CsYss>();
                if (name != "List`1")
                {
                    yss.Add((CsYss)obj);
                }
                else
                {
                    yss = (List<CsYss>)obj;
                }

                AddDataToTable(yss, table);
            }
            #endregion
            #region Dxjx
            else if (type == "Automation.Model.Dxjx")
            {
                List<Dxjx> yss = new List<Dxjx>();
                if (name != "List`1")
                {
                    yss.Add((Dxjx)obj);
                }
                else
                {
                    yss = (List<Dxjx>)obj;
                }

                AddDataToTable(yss, table);
            }
            #endregion
            #region Dwhz
            else if (type == "Automation.Model.Dwhz")
            {
                List<Dwhz> yss = new List<Dwhz>();
                if (name != "List`1")
                {
                    yss.Add((Dwhz)obj);
                }
                else
                {
                    yss = (List<Dwhz>)obj;
                }

                AddDataToTable(yss, table);
            }
            #endregion
            #region FgFltz
            else if (type == "Automation.Model.FgFltz")
            {
                List<FgFltz> yss = new List<FgFltz>();
                if (name != "List`1")
                {
                    yss.Add((FgFltz)obj);
                }
                else
                {
                    yss = (List<FgFltz>)obj;
                }

                AddDataToTable(yss, table);
            }
            #endregion
            #region Fybl
            else if (type == "Automation.Model.Fybl")
            {
                List<Fybl> yss = new List<Fybl>();
                if (name != "List`1")
                {
                    yss.Add((Fybl)obj);
                }
                else
                {
                    yss = (List<Fybl>)obj;
                }

                AddDataToTable(yss, table);
            }
            #endregion
            #region FyBl_Hj
            else if (type == "Automation.Model.FyBl_Hj")
            {
                List<FyBl_Hj> yss = new List<FyBl_Hj>();
                if (name != "List`1")
                {
                    yss.Add((FyBl_Hj)obj);
                }
                else
                {
                    yss = (List<FyBl_Hj>)obj;
                }

                AddDataToTable(yss, table);
            }
            #endregion
            #region Gc_Azfyjs
            else if (type == "Automation.Model.Gc_Azfyjs")
            {
                List<Gc_Azfyjs> yss = new List<Gc_Azfyjs>();
                if (name != "List`1")
                {
                    yss.Add((Gc_Azfyjs)obj);
                }
                else
                {
                    yss = (List<Gc_Azfyjs>)obj;
                }

                AddDataToTable(yss, table);
            }
            #endregion
            #region Hsxx
            else if (type == "Automation.Model.Hsxx")
            {
                List<Hsxx> yss = new List<Hsxx>();
                if (name != "List`1")
                {
                    yss.Add((Hsxx)obj);
                }
                else
                {
                    yss = (List<Hsxx>)obj;
                }

                AddDataToTable(yss, table);
            }
            #endregion
            #region Jjcx
            else if (type == "Automation.Model.Jjcx")
            {
                List<Jjcx> yss = new List<Jjcx>();
                if (name != "List`1")
                {
                    yss.Add((Jjcx)obj);
                }
                else
                {
                    yss = (List<Jjcx>)obj;
                }

                AddDataToTable(yss, table);
            }
            #endregion
            #region JjcxFz
            else if (type == "Automation.Model.JjcxFz")
            {
                List<JjcxFz> yss = new List<JjcxFz>();
                if (name != "List`1")
                {
                    yss.Add((JjcxFz)obj);
                }
                else
                {
                    yss = (List<JjcxFz>)obj;
                }

                AddDataToTable(yss, table);
            }
            #endregion
            #region JjcxItem
            else if (type == "Automation.Model.JjcxItem")
            {
                List<JjcxItem> yss = new List<JjcxItem>();
                if (name != "List`1")
                {
                    yss.Add((JjcxItem)obj);
                }
                else
                {
                    yss = (List<JjcxItem>)obj;
                }

                AddDataToTable(yss, table);
            }
            #endregion
            #region Mbgj
            else if (type == "Automation.Model.Mbgj")
            {
                List<Mbgj> yss = new List<Mbgj>();
                if (name != "List`1")
                {
                    yss.Add((Mbgj)obj);
                }
                else
                {
                    yss = (List<Mbgj>)obj;
                }

                AddDataToTable(yss, table);
            }
            #endregion
            #region MbgjZhys
            else if (type == "Automation.Model.MbgjZhys")
            {
                List<MbgjZhys> yss = new List<MbgjZhys>();
                if (name != "List`1")
                {
                    yss.Add((MbgjZhys)obj);
                }
                else
                {
                    yss = (List<MbgjZhys>)obj;
                }

                AddDataToTable(yss, table);
            }
            #endregion
            #region Plhs
            else if (type == "Automation.Model.Plhs")
            {
                List<Plhs> yss = new List<Plhs>();
                if (name != "List`1")
                {
                    yss.Add((Plhs)obj);
                }
                else
                {
                    yss = (List<Plhs>)obj;
                }

                AddDataToTable(yss, table);
            }
            #endregion
            #region RcjHz
            else if (type == "Automation.Model.RcjHz")
            {
                List<RcjHz> yss = new List<RcjHz>();
                if (name != "List`1")
                {
                    yss.Add((RcjHz)obj);
                }
                else
                {
                    yss = (List<RcjHz>)obj;
                }

                AddDataToTable(yss, table);
            }
            #endregion
            #region RcjHzInfo
            else if (type == "Automation.Model.RcjHzInfo")
            {
                List<RcjHzInfo> yss = new List<RcjHzInfo>();
                if (name != "List`1")
                {
                    yss.Add((RcjHzInfo)obj);
                }
                else
                {
                    yss = (List<RcjHzInfo>)obj;
                }

                AddDataToTable(yss, table);
            }
            #endregion
            #region RcjHzZbcl
            else if (type == "Automation.Model.RcjHzZbcl")
            {
                List<RcjHzZbcl> yss = new List<RcjHzZbcl>();
                if (name != "List`1")
                {
                    yss.Add((RcjHzZbcl)obj);
                }
                else
                {
                    yss = (List<RcjHzZbcl>)obj;
                }

                AddDataToTable(yss, table);
            }
            #endregion
            #region Spt
            else if (type == "Automation.Model.Spt")
            {
                List<Spt> yss = new List<Spt>();
                if (name != "List`1")
                {
                    yss.Add((Spt)obj);
                }
                else
                {
                    yss = (List<Spt>)obj;
                }

                AddDataToTable(yss, table);
            }
            #endregion
            #region SybwGl
            else if (type == "Automation.Model.SybwGl")
            {
                List<SybwGl> yss = new List<SybwGl>();
                if (name != "List`1")
                {
                    yss.Add((SybwGl)obj);
                }
                else
                {
                    yss = (List<SybwGl>)obj;
                }

                AddDataToTable(yss, table);
            }
            #endregion
            #region Tzdj
            else if (type == "Automation.Model.Tzdj")
            {
                List<Tzdj> yss = new List<Tzdj>();
                if (name != "List`1")
                {
                    yss.Add((Tzdj)obj);
                }
                else
                {
                    yss = (List<Tzdj>)obj;
                }

                AddDataToTable(yss, table);
            }
            #endregion
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="table"></param>
        public static void AddDataToTable<T>(List<T> obj, DataGridView table)
        {
            #region 加载表头
            if (obj.Count == 0)
            {
                MessageBox.Show("文件中没有数据！");
                return;
            }
            var result = GetObjProperties(obj[0]);
            foreach (var item in result)
            {
                table.Columns.Add(item.PropertyType.Name, item.Name);
            }
            for (int i = 0; i < table.Columns.Count; i++)
            {
                table.Columns[i].SortMode = DataGridViewColumnSortMode.NotSortable;
                //table.Columns[i].AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells;
            }
            #endregion

            #region 加载数据
            List<string> CanEditValueType = new List<string>() { "Int32", "String", "Decimal", "Boolean" }; //定义可编辑的数据类型
            for (int i = 0; i < obj.Count; i++)   //添加数据 
            {
                var RowData = obj[i];
                var header = table.Columns;
                DataGridViewRow row = new DataGridViewRow();
                for (int j = 0; j < header.Count; j++)
                {
                    string ColName = header[j].HeaderText;
                    Type t = RowData.GetType();
                    var value = t.GetProperty(ColName).GetValue(RowData);
                    if (value == null)
                    { value = "NULL"; }
                    var valueType = value.GetType();
                    DataGridViewTextBoxCell textboxcell = new DataGridViewTextBoxCell();
                    textboxcell.Value = value;
                    row.Cells.Add(textboxcell);

                    if (!CanEditValueType.Contains(valueType.Name)) //如果数据类型不在可编辑中则Cell设置为只读
                    {
                        textboxcell.ReadOnly = true;
                        textboxcell.Style.BackColor = Color.SkyBlue;
                    }
                }
                table.Rows.Add(row);
            }
        }
        #endregion

    }
}
